package com.yiidata.rpc.arpc;

import org.apache.avro.AvroRemoteException;
import org.apache.avro.ipc.HttpServer;
import org.apache.avro.ipc.NettyServer;
import org.apache.avro.ipc.Server;
import org.apache.avro.ipc.specific.SpecificResponder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * <pre>
 *
 * Created by IntelliJ IDEA.
 * User: zhenqin
 * Date: 15/1/6
 * Time: 18:24
 * To change this template use File | Settings | File Templates.
 *
 * </pre>
 *
 * @author zhenqin
 */
public class AvroHelloWorldServer extends SpecificResponder {

    public AvroHelloWorldServer(Class iface, Object impl) {
        super(iface, impl);
    }

    public static void main(String[] args) throws Exception {
        String host = "0.0.0.0";
        int port = 8088;
        // 如果使用 NettyServer 请打开下面的代码
        //Server server = new NettyServer(new AvroHelloWorldServer(
        //        AvroHelloWorldProtocol.class, new AvroHelloWorldProtocolImpl()),
        //        new InetSocketAddress(host, port));

        // 如果 Python 使用，请使用如下的 HttpServer
        Server server = new HttpServer(new AvroHelloWorldServer(
                AvroHelloWorldProtocol.class, new AvroHelloWorldProtocolImpl()),
                host, port);

        server.start();
        System.out.printf("avro rpc server started on : %s:%d\n", host, port);
        server.join();
    }
}

class AvroHelloWorldProtocolImpl implements AvroHelloWorldProtocol {

    @Override
    public Message sayHello(User user) throws AvroRemoteException {
        System.out.println("exec hello, " + user.getName());
        return new Message("hello, " + user.getName());
    }
}
